﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Windows.Forms.Design;

namespace WindowsFormsApplication1
{
    class ColorEditor : System.Drawing.Design.UITypeEditor
    {
        public override void PaintValue(System.Drawing.Design.PaintValueEventArgs e)
        {
            Image i= Properties.Resources.clrG;;
            switch((int)e.Value )
            {
                case 1: i= Properties.Resources.clrY; break;
                case 2: i= Properties.Resources.clrR; break;
            }
            e.Graphics.DrawImage(i, 0, 0, e.Bounds.Width, e.Bounds.Height);
        }
        public override bool GetPaintValueSupported(System.ComponentModel.ITypeDescriptorContext context) 
            {  return true;  }
        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context )
        {  return UITypeEditorEditStyle.DropDown;  }
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            if (value.GetType() != typeof(float) && value.GetType() != typeof(int)) 
                return value;
            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if( edSvc != null ) {
                ColorControl colorControl = new ColorControl((int)value);
                edSvc.DropDownControl( colorControl );
                if( value.GetType() == typeof(double) )        
                    return colorControl.Color;
                else if( value.GetType() == typeof(float) )   
                    return (float)colorControl.Color;
                else if( value.GetType() == typeof(int) )  
                    return (int)colorControl.Color;}
            return value;
        }
    }
}
